//要素基类
import {
    Serialize,
    SerializeMetaKey,
    Children,
    ChildrenMetaKey,
    AliasMetaKey,
    ColumnMetaKey, EnumMetaKey, EditorMetaKey, RelateMetaKey, Editor, Alias
} from "./decorator";
import {Element} from './element';
import {Meta} from "./meta";
import {IRegister} from "./interface";
import {ProductCategory, ProductCatalog} from "./product/element/category";


export class ProductConfig extends Element implements IRegister{

    //register singleton
    private static _instance: ProductConfig;
    static getInstance(): ProductConfig {
        if (!ProductConfig._instance) {
            ProductConfig._instance = new ProductConfig();
        }
        return ProductConfig._instance;
    }

    getCollection(type): Array<Element> {
        switch (type.name || type) {
            case 'ProductCatalog':
                return this.catalogs.sort((a, b) => a.order - b.order);          
            default:
                return [];
        }
    }

    // this is ridiculous , just for update nz-table....
    setCollection(type, collection) {
        switch (type.name || type) {
            case 'ProductCatalog':
                this.catalogs = collection;
                break;
        }
    }

    @Children(ProductCatalog)
    @Serialize()
    public catalogs: Array<ProductCatalog> = [];

    public metaMap: Map<string, Array<Meta>> = new Map<string, Array<Meta>>();

    constructor() {
        super();
    }

    init() {
        this.catalogs = [];
    }

    initMeta(){
        this.metaMap = new Map<string, Array<Meta>>();
        this.addMeta(ProductCatalog, "ProductCatalog");
    }

    addMeta(type, name) {
        const metas = new Array<Meta>();
        const element = new type();
        Object.keys(element).forEach( property => {
            const meta = new Meta();
            meta.init(element, property);
            metas.push(meta);
        });
        this.metaMap.set(name, metas);
    }

    relate(): void {

    }

    getColumns(type: string) {
        return this.metaMap.get(type).filter(meta => meta.column);
    }

    getType(type) {
        switch (type) {
            case 'ProductCatalog':
                return ProductCatalog;
            default:
                return undefined;
        }
    }

}


